package com.dswz.controller.pc;

import com.dswz.po.User;
import com.dswz.po.UserOrder;
import com.dswz.service.SessionService;
import com.dswz.service.UserOrderService;
import com.dswz.service.UserService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.EncryptUtil;
import com.dswz.utils.PageBean;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/pcs" })
public class UserOrderPcController {
	private Logger logger = LogManager.getLogger(UserOrderPcController.class);

	@Autowired
	private UserOrderService userOrderService;

	@Autowired
	private UserService userService;

	@Autowired
	private SessionService sessionService;

	@RequestMapping(value = { "/getUserOrderList" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object getUserOrderList(String userId, String userOrderId, Byte userOrderState, String userOrderCreateTime,
			Integer page, Integer rows, Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userId:" + userId + "userOrderId:" + userOrderId + "userOrderState:" + userOrderState
					+ "userOrderCreateTime:" + userOrderCreateTime + "page:" + page + "limit:" + rows + "页面来源:"
					+ request.getHeader("referer") + "客户端:" + request.getHeader("user-agent") + " 客户端ip: "
					+ Utils.getUserRealAddress(request) + " 操作人: " + username);
			User user = this.userService.getUserById(userId);
			PageBean pb = this.userOrderService.getUserOrderList(username, user.getUserId(), null, null, userOrderId,
					userOrderState, userOrderCreateTime, null, Byte.valueOf((byte) 0), page, rows);
			result.setResult(0);
			result.setMessage("OK");
			result.setRows(pb.getContentList());
			result.setTotal(pb.getTotalRecords());
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			result.setResult(2);
			result.setMessage(e.getMessage());
			this.logger.error(e.getMessage(), e);
		}
		return result;
	}

	@RequestMapping(value = { "/getPayInfo" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object getPayInfo(String userId, String goodsId, String goodsTitle, Double goodsPrice,
			Integer userOrderBuyNum, String userOrderMoney, String value, String userOrderReceiverName,
			String userOrderReceiverTelephone, String userOrderReceiverProvince, String userOrderReceiverCity,
			String userOrderReceiverCounty, String userOrderReceiverAddress, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userId:" + userId + "goodsId:" + goodsId + "goodsTitle:" + goodsTitle + "goodsPrice:"
					+ goodsPrice + "userOrderBuyNum:" + userOrderBuyNum + "userOrderMoney:" + userOrderMoney + "value:"
					+ value + "userOrderReceiverName:" + userOrderReceiverName + "userOrderReceiverTelephone:"
					+ userOrderReceiverTelephone + "userOrderReceiverProvince:" + userOrderReceiverProvince
					+ "userOrderReceiverCity:" + userOrderReceiverCity + "userOrderReceiverCounty:"
					+ userOrderReceiverCounty + "userOrderReceiverAddress:" + userOrderReceiverAddress + "页面来源:"
					+ request.getHeader("referer") + "客户端:" + request.getHeader("user-agent") + " 客户端ip: "
					+ Utils.getUserRealAddress(request) + " 操作人: " + username);
			User user = this.userService.getUserById(userId);
			if (user == null) {
				this.logger.error("用户id不正确!");
				result.setMessage("用户id不正确!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(goodsId)) {
				this.logger.error("商品id为空!");
				result.setMessage("商品id为空!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(goodsTitle)) {
				this.logger.error("请输入商品名!");
				result.setMessage("请输入商品名!");
				result.setResult(3);
				return result;
			}
			if (goodsPrice == null) {
				this.logger.error("请填写商品价格!");
				result.setMessage("请填写商品价格!");
				result.setResult(3);
				return result;
			}
			if (userOrderBuyNum == null) {
				this.logger.error("请填写购买数量!");
				result.setMessage("请填写购买数量!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderMoney)) {
				this.logger.error("请输入每条订单金额!");
				result.setMessage("请输入每条订单金额!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(value)) {
				this.logger.error("请输入总金额!");
				result.setMessage("请输入总金额!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderReceiverName)) {
				this.logger.error("请输入收货人!");
				result.setMessage("请输入收货人!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderReceiverTelephone)) {
				this.logger.error("请输入收货人手机号!");
				result.setMessage("请输入收货人手机号!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderReceiverProvince)) {
				this.logger.error("请输入所在省!");
				result.setMessage("请输入所在省!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderReceiverCity)) {
				this.logger.error("请输入所在市!");
				result.setMessage("请输入所在市!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderReceiverCounty)) {
				this.logger.error("请输入所在县!");
				result.setMessage("请输入所在县!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userOrderReceiverAddress)) {
				this.logger.error("请填写详细地址!");
				result.setMessage("请填写详细地址!");
				result.setResult(3);
				return result;
			}
			String parter = "31cb23272b944f4183501cc047e3c4ab";
			String key = "C094DAD5AB2DC7E5BAA41C8470A0D515";
			String orderid = UUID.randomUUID().toString().replaceAll("-", "");
			String callbackurl = "http://www.liyingao.com/cross_origin/cxding/notify";

			String hrefbackurl = "http://p.cxding.com/payment_success.html";
			String str = "parter=%s&value=%s&orderid=%s&key=%s";
			String strTemp = String.format(str, new Object[] { parter, value, orderid, key });
			String sign = EncryptUtil.encodeByMD5(strTemp);
			Map<String, String> map = new HashMap();

			String payInfo = "parter=" + parter + "&value=" + value + "&orderid=" + orderid + "&callbackurl="
					+ callbackurl + "&hrefbackurl=" + hrefbackurl + "&sign=" + sign;
			map.put("payInfo", payInfo);

			UserOrder userOrder = new UserOrder();
			userOrder.setUserId(userId);
			userOrder.setUserName(user.getUserName());
			userOrder.setUserMobile(user.getUserMobile());

			userOrder.setUserOrderId(orderid);
			userOrder.setUserOrderBuyNum(userOrderBuyNum);
			userOrder.setUserOrderMoney(Double.valueOf(userOrderMoney));
			userOrder.setUserOrderState(Byte.valueOf((byte) 0));
			userOrder.setGoodsId(goodsId);
			userOrder.setGoodsName(goodsTitle);
			userOrder.setGoodsPrice(goodsPrice);
			userOrder.setUserOrderReceiverName(userOrderReceiverName);
			userOrder.setUserOrderReceiverTelephone(userOrderReceiverTelephone);
			userOrder.setUserOrderReceiverProvince(userOrderReceiverProvince);
			userOrder.setUserOrderReceiverCity(userOrderReceiverCity);
			userOrder.setUserOrderReceiverCounty(userOrderReceiverCounty);
			userOrder.setUserOrderReceiverAddress(userOrderReceiverAddress);
			this.userOrderService.insertUserOrder(userOrder);

			result.setData(map);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
